package com.zzq.my.jetpacks.jectpackapp.ui.act

import androidx.recyclerview.widget.GridLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseActivity
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.adapter.RvMenuItemAdapter
import com.zzq.my.jetpacks.jectpackapp.databinding.ActivityRecyleviewGriveMenuBinding
import com.zzq.my.jetpacks.jectpackapp.decoration.GridSpaceItemDecoration
import com.zzq.my.jetpacks.jectpackapp.utils.ABPixelUtil
import com.zzq.my.jetpacks.jectpackapp.view.decoration.MyDividerItem

/**
 * 描述：ReCyleView 间距
 * 作者：zhuangzeqin
 * 时间: 2022/1/18-17:04
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class RecyleViewMenuAct : BaseActivity(R.layout.activity_recyleview_grive_menu) {
    private val vb by viewBinding(ActivityRecyleviewGriveMenuBinding::bind)

    private val dataList: MutableList<String> = mutableListOf()
    private lateinit var mRvMenuItemAdapter: RvMenuItemAdapter

    /**
     * 初始化数据
     */
    override fun initData() {
//        if (::mRvMenuItemAdapter.isInitialized)
        mRvMenuItemAdapter = RvMenuItemAdapter()
//        val moredata = layoutInflater.inflate(R.layout.item_moredata, null)
//        val layoutParams = ViewGroup.LayoutParams(
//            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT
//        )
//        moredata.layoutParams = layoutParams

        dataList.apply {
            add("商户管理")
            add("代理管理")
            add("机具管理")
            add("交易查询")

            add("扩展代理")
            add("政策模版")
            add("政策模版2")
            add("政策模版3")

            add("政策模版4")
            add("政策模版5")

        }
        mRvMenuItemAdapter.setList(dataList)
        vb.rvMenu.adapter = mRvMenuItemAdapter
        val spanCount = 4; // 3 columns
        val spacing = 50; // 50px
        val includeEdge = false;//设置最外两侧边距
        vb.rvMenu.addItemDecoration(MyDividerItem(ABPixelUtil.dp2px(15f, this),ABPixelUtil.dp2px(18f, this)))
//        val itemDecoration = GridSpaceItemDecoration(ABPixelUtil.dp2px(15f, this), true)
        val gridLayoutManager = vb.rvMenu.layoutManager as GridLayoutManager
        gridLayoutManager.spanCount = 4//一行 4个
        gridLayoutManager.spanSizeLookup = object :GridLayoutManager.SpanSizeLookup(){

            override fun getSpanSize(position: Int): Int {
                //最后一个  和第一个 占据一行
                val totalCount =dataList.size//列表的总个数
                return if(position==totalCount-1||position==totalCount-2) 2 else 1//最后2个平均分
            }
        }
        mRvMenuItemAdapter.setOnItemClickListener{
                adapter, view, position ->
//            vb.circleProgress.progress = 90
            vb.circleProgress.setUpToStandardColor(R.color.color_3887EB,R.color.color_EBF3FD)
        }
//        vb.rvMenu.addItemDecoration(itemDecoration)
//        vb.rvMenu.addItemDecoration(GridSpacingItemDecoration(spanCount, spacing, includeEdge))
//        vb.rvMenu.addItemDecoration(GridSpaceDecoration(this,22))
//        mRvGrowUpAdapter.addFooterView(moredata, -1, LinearLayout.HORIZONTAL)
//        mRvGrowUpAdapter.footerLayout?.setOnClickListener {
//            showMsg("aaaa")
//        }

//        vb.rvMenu.post {
//            val width =  vb.rvMenu.getWidth();//rv 的宽度
//            val height =  vb.rvMenu.getMeasuredHeight();//rv 测量的高度
//            var itemWidth = 0;
//            var itemHeight = 0;
//            val totalCount = vb.rvMenu.childCount//列表的总个数
//            try {
//                itemWidth = vb.rvMenu.layoutManager?.getChildAt(0)?.width ?: 0;
//                itemHeight = vb.rvMenu.layoutManager?.getChildAt(0)?.height?:0;
//            }catch (e:NullPointerException ){
//                e.printStackTrace();
//            }
//            var space = (width - spanCount * itemWidth) / (spanCount - 1);
//            vb.rvMenu.addItemDecoration(GridSpaceCenterDecoration(totalCount, width, itemWidth,
//                space)
//            );
//
//
//        }


    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    fun dip2px(dpValue: Float): Int {
        val scale = mContext.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    /**
     * 初始化操作
     */
    override fun initView() {

    }

    /**
     * 初始化点击事件操作
     */
    override fun eventOnClick() {

    }

//    /**
//     * 将RecyclerView的网格布局中的某个item设置为独占一行、只占一列，只占两列、等等
//     * @param gridLayoutManager
//     */
//    private void setSpanCount(GridLayoutManager gridLayoutManager){
//        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//            @Override
//            public int getSpanSize(int position) {
//                int type = getItemViewType(position);
//                if (type == TYPE_NORMAL_ITEM) {
//                    return 1;//只占一行中的一列，
//                } else {
//                    return gridLayoutManager.getSpanCount();//独占一行
//                }
//            }
//        });
//    }

}